﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using DTO;
using AutoMapper;

namespace DataAccess
{
    public static class PositionDAO
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static PositionDAO()
        {
            XmlConfigurator.Configure();
        }

        public static List<PositionDTO> GetPositionList()
        {
            try
            {
                List<PositionDTO> lstPosition = new List<PositionDTO>();

                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = from Position in context.POSITION_LIST
                            select Position;
                foreach (var item in query)
                {
                    PositionDTO dtoPosition = new PositionDTO();
                    dtoPosition = Mapper.Map<POSITION , PositionDTO>(item);
                    lstPosition.Add(dtoPosition);
                }
                return lstPosition;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }

        public static bool AddNewPosition(PositionDTO dtoPosition)
        {
            try
            {
                POSITION  obj = new POSITION ();
                //Mapping                
                obj = Mapper.Map<PositionDTO, POSITION >(dtoPosition);
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                context.AddToPOSITION_LIST (obj);
                context.SaveChanges(true);

                dtoPosition .POSITION_ID  = obj.POSITION_ID ;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }


        public static bool UpdatePosition(PositionDTO dtoPosition)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

                POSITION  Position = context.POSITION_LIST.First(g => g.POSITION_ID  == dtoPosition.POSITION_ID );               
                
                Position.POSITION_DESC  = dtoPosition.POSITION_DESC ;
                context.SaveChanges(true);
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAPosition(PositionDTO dtoPosition)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                POSITION  Position = context.POSITION_LIST.First(g => g.POSITION_ID  == dtoPosition.POSITION_ID );
                context.DeleteObject(Position);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAllPosition()
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = (from Position in context.POSITION_LIST
                             select Position);
                foreach (var item in query)
                {
                    context.DeleteObject(item);
                }
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }
    }
}
